package com.office.personalFolders.sharedFolder.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.office.personalFolders.myFolder.dao.IsFolderDao;
import com.office.personalFolders.myFolder.pojo.OaFile;
import com.office.personalFolders.myFolder.pojo.OaFolder;
import com.office.personalFolders.myFolder.pojo.TbStaff;
import com.office.personalFolders.myFolder.service.IsFolderService;
import com.office.personalFolders.myFolder.service.impl.folderService;
import com.office.personalFolders.sharedFolder.service.IsSharedFolder;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @project_name: OA
 * @description:
 * @author: LaoWang
 * @date: 2022/10/30 9:30
 */

@Service
public class sharedFolderService implements IsSharedFolder {

    @Autowired
    private IsFolderDao isFolderDao;

    public IsFolderDao getIsFolderDao() {
        return isFolderDao;
    }
    public void setIsFolderDao(IsFolderDao isFolderDao) {
        this.isFolderDao = isFolderDao;
    }

    /**
     * 根据条件查询所有文件夹集合
     * @param oaFolder 查询条件
     * @return 文件夹集合
     */
    public List<OaFolder> getAllFoldersByFolder(OaFolder oaFolder){
        return isFolderDao.getAllFoldersByFolder(oaFolder);
    }


    /**
     * 根据条件查询所有文件夹集合进行分页
     * @param oaFolder 查询条件
     * @return 文件夹集合
     */
    public PageModel<OaFolder> getOaFolderListByFY(OaFolder oaFolder, PageModel<OaFolder> pageModel){
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?2:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        List<OaFolder> userList = getAllFoldersByFolder(oaFolder);


        PageInfo<OaFolder> pageInfo = new PageInfo<OaFolder>(userList);

        pageModel.setData(new ArrayList<OaFolder>(userList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }



    /**
     * 查询指定文件夹下的文件
     * @param oaFile 查询条件（文件夹id）
     * @return 文件集合
     */
    public List<OaFile> getOaFile(OaFile oaFile){
        return isFolderDao.getOaFile(oaFile);
    }



    /**
     * 返回上一级目录
     * @param oaFolder 查询条件
     * @return ..
     */
    public String goPreContext(OaFolder oaFolder,HttpSession session){
        OaFolder oaFolder1 = getAllFoldersByFolder(oaFolder).get(0);

        session.setAttribute("folderId",oaFolder1.getFolderParentId());
        return "true";
    }




    /**
     * 查询指定文件夹下的文件,文件夹
     * @param oaFile,oaFolder 查询条件
     * @return 文件,文件夹集合
     */
    public List<Object> getOaFile(OaFolder oaFolder, OaFile oaFile, HttpSession session){
        String folderId = (String) session.getAttribute("folderId");
        TbStaff login = (TbStaff) session.getAttribute("login");
        oaFolder.setEmpName(login.getStaffId());
        oaFolder.setFolderParentId(folderId);
        List<OaFolder> allFoldersByFolder =getAllFoldersByFolder(oaFolder);

        oaFile.setFolderId(folderId);
        List<OaFile> oaFile1 =getOaFile(oaFile);

        List<Object> list=new ArrayList<>();
        list.add(allFoldersByFolder);
        list.add(oaFile1);
        return list;
    }



    /**
     * 验证指定文件夹下的文件名是否唯一
     * @param oaFolder 验证条件（文件夹id）
     * @return 是否唯一
     */
    public String folderNameYZ(OaFolder oaFolder){
        //查询出该文件夹同级所有文件夹
        OaFolder dq=new OaFolder();
        dq.setFolderId(oaFolder.getFolderId());
        //当前文件夹
        OaFolder oaFolder1 = getAllFoldersByFolder(dq).get(0);

        //同级文件夹
        OaFolder tj=new OaFolder();
        tj.setFolderParentId(oaFolder1.getFolderParentId());
        List<OaFolder> allFoldersByFolder = getAllFoldersByFolder(tj);

        String flag="true";
        for (OaFolder folder : allFoldersByFolder) {
            if (folder.getFolderName().equals(oaFolder.getFolderName()) && !folder.getFolderName().equals(oaFolder1.getFolderName())) {
                flag = "false";
                break;
            }
        }
        return flag;

    }



    /**
     * 查询当前文件夹的父级文件夹所剩大小
     * @param oaFolder 查询条件（文件夹id）
     * @return 剩余大小
     */
    public String getRemainSize(OaFolder oaFolder){
        //查询出该文件夹同级所有文件夹
        OaFolder dq=new OaFolder();
        dq.setFolderId(oaFolder.getFolderId());
        //当前文件夹
        OaFolder oaFolder1 = getAllFoldersByFolder(dq).get(0);

        //如果是最顶级文件夹则可以设定任意大小
        if(oaFolder1.getFolderParentId().equals("0")){
            return "anySize";
        }else {
            //同级文件夹
            OaFolder tj=new OaFolder();
            tj.setFolderParentId(oaFolder1.getFolderParentId());
            List<OaFolder> allFoldersByFolder = getAllFoldersByFolder(tj);

            //计算出同级文件夹大小
            int size=0;
            for (OaFolder folder : allFoldersByFolder) {
                if(folder.getFolderId().equals(oaFolder.getFolderId())){
                    continue;
                }
                size+=Integer.parseInt(folder.getFolderSize());
            }

            //父级文件夹大小
            OaFolder fj=new OaFolder();
            fj.setFolderId(oaFolder1.getFolderParentId());
            OaFolder oaFolder2 = getAllFoldersByFolder(fj).get(0);

            return String.valueOf(Integer.parseInt(oaFolder2.getFolderSize())-size);
        }


    }




    /**
     * 增加一个文件夹
     * @param oaFolder 增加的文件夹
     * @return 影响行数
     */
    public Integer addFolder(OaFolder oaFolder){
        return isFolderDao.addFolder(oaFolder);
    }


    //修改文件夹（名字、删除状态、共享状态）
    public Integer updateFolder(OaFolder oaFolder){
        return isFolderDao.updateFolder(oaFolder);
    }


    /**
     * 文件夹修改（更名和管理员设置文件大小和设置共享）
     * @param oaFolder 修改的文件夹
     * @return 影响行数
     */
    public Integer updateFolder1(OaFolder oaFolder){
        return isFolderDao.updateFolder1(oaFolder);
    }




    /**
     * 删除文件夹（修改可用状态即可）
     * @param oaFolder 删除的文件夹
     * @return 影响行数
     */
    public Integer delete(OaFolder oaFolder){
        return updateFolder(oaFolder);
    }


}
